<?php

namespace Shared\LRC;

/**
 * @author Jordan Minchev <j.minchev@object.bg>
 * @copyright (c) 2014, ASPECT.BG Ltd.
 */
class LRCSupport {

    /**
     * @var \Shared\Main\Language
     */
    private $CurrentLanguage;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $EntityManager;

    public function __construct() {
        $this->EntityManager = \Shared\Application::instance()->getEntityManager();
        $this->initConfiguration($this->EntityManager->getEventManager());
    }

    private function initConfiguration($eventManager) {
        //load annotations
        \Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
                __DIR__ . '/Annotations/Loader/AnnotationsLoader.php'
        );
        //register even subsriber
        $eventManager->addEventSubscriber(new \Shared\LRC\Event\EventSubscriber);
    }

    /**
     * 
     * @param string $locale
     * @throws LanguageNotFoundException
     */
    public function initCurrentLanguage($locale = null) {
        if ($locale) {

            $Query = $this->EntityManager->createQuery('SELECT language 
						FROM Shared\Main\Language language
						WHERE language.slug = :slug')
                    ->setParameter('slug', $locale);

            if ($Query->getResultCacheDriver()) {
                $Query->useResultCache(true, 86400);
            }

            $Language = $Query->getOneOrNullResult();

            if ($Language) {
                $this->setCurrentLanguage($Language);
            } else {
                throw new LanguageNotFoundException('No such locale!');
            }
        } else {
            $this->setCurrentLanguage($this->getDefaultLanguage());
        }
    }

    /**
     * 
     * @return \Shared\Main\Language
     */
    public function getCurrentLanguage() {
        if (!$this->CurrentLanguage) {
            $this->CurrentLanguage = $this->getDefaultLanguage();
        }
        
        return $this->CurrentLanguage;
    }

    public function setCurrentLanguage(\Shared\Main\Language $CurrentLanguage) {
        $this->CurrentLanguage = $CurrentLanguage;
    }

    /**
     * 
     * @return \Shared\Main\Language
     * @throws NoDefaultLanguageException
     */
    public function getDefaultLanguage() {

        $Query = $this->EntityManager->createQuery('SELECT language 
						FROM Shared\Main\Language language
						WHERE language.isDefault = 1');

        if ($Query->getResultCacheDriver()) {
            $Query->useResultCache(true, 86400);
        }

        $Language = $Query->getOneOrNullResult();
        if (!$Language) {
            throw new NoDefaultLanguageException();
        }
        return $Language;
    }

    /**
     * @return array
     */
    public function getLanguages() {
        $Query = $this->EntityManager->createQuery('SELECT language 
						FROM Shared\Main\Language language');

        if ($Query->getResultCacheDriver()) {
            $Query->useResultCache(true, 86400);
        }

        return $Query->getResult();
    }

    /**
     * 
     * @param \Shared\LRC\Entity\TranslatableI $Object
     * @return \Shared\LRC\Manager
     */
    public function getManager(\Shared\LRC\Entity\TranslatableI $Object) {
        $Manager = new Manager($Object);
        return $Manager;
    }

    public function get(\Shared\LRC\Entity\TranslatableI $Object, $property, \Shared\Main\Language $Language = null) {
        return $this->getManager($Object)->get($property, $Language);
    }

}
